package DBLayer;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import ModelLayer.RegistrationSchema;
import ModelLayer.ResponseList;

public class DBRegistrationSchema implements DBRegistrationSchemaIF
{
private Connection con;
    
    public DBRegistrationSchema()
    {
    	con = DBConnection.getInstance().getDBcon();
    }
	
    public ArrayList<RegistrationSchema> getAllRegistrationSchemas(boolean retriveAssociation)
    {
        return miscWhere("", retriveAssociation);
    }

    public RegistrationSchema searchRegistrationSchemaID(int registrationSchemaID, boolean retriveAssociation)
    {   
    	String wClause = "RegistrationSchemaID = " + registrationSchemaID + "";
        return singleWhere(wClause, retriveAssociation);
    }
    
    public RegistrationSchema searchRegistrationSchemaByResponseListID(int responseListID, boolean retriveAssociation)
    {   
    	String wClause = "ResponseListID = " + responseListID + "";
        return singleWhere(wClause, retriveAssociation);
    }
    
    private ArrayList<RegistrationSchema> miscWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		ArrayList<RegistrationSchema> registrationSchemaList = new ArrayList<RegistrationSchema>();	
		
		String query = buildQuery(wClause);
  
  		try
  		{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
			while( results.next() )
			{
				RegistrationSchema rsObj = new RegistrationSchema();
				rsObj = buildRegistrationSchema(results);	
				registrationSchemaList.add(rsObj);	
			}
			stmt.close();
		}
	 	catch(Exception e)
	 	{
	 		System.out.println("Query exception - select: "+e);
			e.printStackTrace();
	 	}
		return registrationSchemaList;
	}
	
	private RegistrationSchema singleWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		RegistrationSchema rsObj = new RegistrationSchema();
	    String query = buildQuery(wClause);
//        System.out.println(query);
		try
		{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
	 		if( results.next() )
	 		{
	 			rsObj = buildRegistrationSchema(results);
	 			stmt.close();
			}
		}	
	 	catch(Exception e)
	 	{
	 		System.out.println("Query exception: "+e);
	 	}
		return rsObj;
	}

	private String buildQuery(String wClause)
	{
	    String query="SELECT * FROM RegistrationSchema";
		if (wClause.length()>0)
			query=query+" WHERE "+ wClause;		
		return query;
	}

	private RegistrationSchema buildRegistrationSchema(ResultSet results)
      {   
		RegistrationSchema rsObj = new RegistrationSchema();
          try
          {
                rsObj.setRegistrationSchemaID(results.getInt("RegistrationSchemaID"));
                rsObj.setRegistration(results.getString("Registration"));
                rsObj.setPmRight(results.getBoolean("PMRight"));
                rsObj.setPmPrompt(results.getBoolean("PMPrompt"));
                rsObj.setPmWrong(results.getBoolean("PMWrong"));
                rsObj.setResponseList(new ResponseList(results.getInt("ResponseListID")));
          }
         catch(Exception e)
         {
             System.out.println("error in building the RegistrationSchema object");
         }
         return rsObj;
      }
}
